﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Ue.Qcdz.Domain.ContentTypes;

namespace Ue.Qcdz.Domain.ContentTypes
{
    public class ContentTypeService
    {
        #region 单例

        private static class ContentTypeServiceHolder
        {
            public static ContentTypeService service = new ContentTypeService();
        }

        public static ContentTypeService Instance
        {
            get { return ContentTypeServiceHolder.service; }
        }

        #endregion

        #region 构造、字段

        private IDictionary<object, ContentType> contentTypesCache;

        private ContentTypeService()
        {
            this.contentTypesCache = new Dictionary<object, ContentType>();
        }

        #endregion

        public ContentType RegisterContentType(ContentType contentType)
        {
            this.contentTypesCache.Add(contentType.Key,contentType);

            return contentType;
        }

        public ReadOnlyCollection<ContentType> Items
        {
            get { return new ReadOnlyCollection<ContentType>(this.contentTypesCache.Values.ToList()); }
        }

        public ContentType FindBy(object key)
        {
            return this.Items.SingleOrDefault(item => item.Key.ToString().ToLower() == key.ToString().ToLower());
        }
    }
}
